package main

import (
	. "GO_data_structures_and_algorithms/tool"
	"fmt"
	"math/rand"
)

func mostLeftNoLessNumIndex(arr []int, num int) int {

	if arr == nil || len(arr) == 0 {
		return -1
	}
	L := 0
	R := len(arr) - 1
	ans := -1
	for L <= R {
		mid := (L + R) / 2
		if arr[mid] >= num {
			ans = mid
			R = mid - 1
		} else {
			L = mid + 1
		}
	}
	return ans
}
func mostRightNoBigerNumIndexarr(arr []int, num int) int {
	if arr == nil || len(arr) == 0 {
		return -1
	}
	L := 0
	R := len(arr) - 1
	ans := -1
	for L <= R {
		mid := (L + R) / 2
		if arr[mid] <= num {
			ans = mid
			L = mid + 1
		} else {

			R = mid - 1
		}
	}
	return ans

}
func testmostLeftNoLessNumIndex(arr []int, value int) int {
	ans := -1
	for i := 0; i < len(arr); i++ {
		if arr[i] >= value {
			ans = i
		}
	}
	return ans
}
func testmostRightNoBigerNumIndexarr(arr []int, value int) int {
	ans := -1
	for i := 0; i < len(arr); i++ {
		if arr[i] <= value {
			ans = i
		}
	}
	return ans
}

func main() {
	//对数器
	testTime := 100231
	maxSize := 10
	maxValue := 100
	succed := true

	for i := 0; i < testTime; i++ {
		_, arr := LenRandomValueRandom(maxSize, maxValue)

		SelectSort(arr)
		value := rand.Intn(maxValue+1) - rand.Intn(maxValue)
		indexarr := testmostRightNoBigerNumIndexarr(arr, value)
		numIndexarr := mostRightNoBigerNumIndexarr(arr, value)
		if indexarr != numIndexarr {
			fmt.Println(indexarr, numIndexarr, arr)
			succed = false
		}

	}
	if succed {
		fmt.Println("成功了")
	} else {
		fmt.Println("算法还得改进")
	}
}
